package com.danceforyou.user.service.impl;

import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.impl.BaseServiceImpl;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.common.util.StringUtil;
import com.danceforyou.user.dao.UserMenuHiddenDAO;
import com.danceforyou.user.model.UserDO;
import com.danceforyou.user.model.UserMenuHiddenDO;
import com.danceforyou.user.service.UserMenuHiddenService;
import com.danceforyou.user.vo.UserMenuHiddenVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
* 类名称：UserMenuHiddenServiceImpl
* 创建人：danceforyou
* 创建时间：2021年01月12日
* 版本号：1.0v
*/
@Service("userMenuHiddenService")
public class UserMenuHiddenServiceImpl extends BaseServiceImpl<Long, UserMenuHiddenDO, UserMenuHiddenVO, UserMenuHiddenDAO> implements UserMenuHiddenService {
    @Autowired
    private UserMenuHiddenDAO userMenuHiddenDAO;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public UserMenuHiddenDAO getDao() {
        return userMenuHiddenDAO;
    }

    @Override
    public void saveInfo(UserMenuHiddenDO userMenuHidden) {
        this.insert(userMenuHidden);
    }

    @Override
    public void updateInfo(UserMenuHiddenDO userMenuHidden) {
        this.update(userMenuHidden);
    }

    @Override
    public int deleteInfoByIds(String ids) {
        String[] idArr = ids.split(",");
        int i = 0;
        for (String id : idArr) {
            i += this.deleteById(id);
        }
        return i;
    }

    @Override
    public void saveUserMenuHidden(String menuId, String gridId, String gridCol) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        String[] grid_col_name_arr = gridCol.split(",");
        //先删除已有的设置信息
        userMenuHiddenDAO.deleteByCondition(new HashMap<String, Object>(2){{
            put("menuId", menuId);
            put("gridId", gridId);
        }});
        //保存新的配置
        UserMenuHiddenDO userMenuHidden = new UserMenuHiddenDO();
        userMenuHidden.setUserId(loginUser.getId());
        userMenuHidden.setMenuId(menuId);
        userMenuHidden.setGridId(gridId);
        for (String col_name : grid_col_name_arr) {
            if (StringUtil.isBlank(col_name)) {
                continue;
            }
            userMenuHidden.setId(null);
            userMenuHidden.setGridCol(col_name);
            userMenuHiddenDAO.insert(userMenuHidden);
        }
    }
}
